﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections;
public partial class gupiaochi_RedPool : System.Web.UI.Page
{
    public string getList = "";
    public int redcount = 0; //入选红股票池的数量
    public int szcount = 0; //上涨数量
    public int xdcount = 0; //下跌数量
    public int count10 = 0; //涨幅超过10%的数量
    public string CGL = "0"; //成功率
    public string strPage = null;
    public string strStime = "";
    public string strEtime = "";
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            var begin_time = Request["txtSDate"];
            var end_time = Request["txtEDate"];
            if (begin_time == "" || begin_time==null)
            {
                begin_time = "";
                if (end_time == "" || end_time == null)
                {
                    end_time = "";
                }
            }
            else
            {
                if (end_time == "" || end_time == null)
                {
                    end_time = "";
                }
            }

            getList = BindDate(begin_time.ToString(), end_time.ToString());
            strStime = begin_time.ToString();
            strEtime = end_time.ToString();
        }
       
    }

    //入选红股票池的股票
    public string BindDate(string begintime,string endtime)
    {  
        StringBuilder sb = new StringBuilder();
        string url = UrlGet.Get("test/73proxy.ashx?id=2&t=1&p=1&i=null&w=460&h=302&c=8&json=1&pj=hero");
        var tmplst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockEntity>>(url);
        sb.AppendLine("<table  width=\"548px\" cellpadding=\"0\" cellspacing=\"0\" text-align=\"center\">");
        if (tmplst == null)
        {
            sb.AppendLine("<tr colspan=\"5\"><td  style=\"padding-left:230px;padding-top:60px\">--暂无相关数据--</td></tr>");
            sb.AppendLine("</table>");
            return sb.ToString();
        }
        try
        {
            var _lst = tmplst.ToList();
            if (begintime == "")
            {
                if (endtime != "")
                {
                    DateTime etime = Convert.ToDateTime(endtime);
                    _lst = _lst.Where(x => x.Ptime == etime).ToList();
                }
               
            }
            else
            {
                DateTime btime1 = Convert.ToDateTime(begintime);

                if (endtime != "")
                {
                    DateTime etime1 = Convert.ToDateTime(endtime);
                    _lst = _lst.Where(x => x.Ptime >= btime1 && x.Ptime <= etime1).ToList();
                }//两者不为空
                else
                {
                    _lst = _lst.Where(x => x.Ptime == btime1).ToList();
                }
               
            }

           
            _lst = _lst.OrderByDescending(x => x.Ptime).ToList(); //降序排列
            
            var count = _lst.Count();
            List<StockList2> list = new List<StockList2>();
            ArrayList arr = new ArrayList();

            int _totalCount = 0;
            int _pagesize = 6;
            int _totalPage = 0;
            int page = ToInt(Request["page"], 1);

            for (int i = 0; i < _lst.Count(); i++)
            {
                if (begintime == "" && endtime == "")
                {
                    for (int j = 0; j < _lst[0].d.Count; j++)
                    {
                        StockList2 et = new StockList2();
                        et.C = _lst[0].d[j].C;
                        et.N = _lst[0].d[j].N;
                        et.JD = _lst[0].d[j].JD;
                        et.ZD = _lst[0].d[j].ZD;
                        et.Stime = _lst[0].Ptime;
                        list.Add(et);
                       
                    }
                    redcount = list.Count();
                    szcount = list.Where(x => x.ZD > 0).ToList().Count();  //上涨数量
                    xdcount = list.Where(x => x.ZD < 0).ToList().Count();  //下跌数量
                    count10 = list.Where(x => x.ZD > 10).ToList().Count(); //最大涨幅超过100%
                    CGL = ((Convert.ToDouble(szcount) / Convert.ToDouble(redcount)) * 100).ToString("f");  //成功率

                    break;
                } //最近次的红蓝股票池
                for (int j = 0; j < _lst[i].d.Count; j++)
                {

                    StockList2 et1 = new StockList2();
                    et1.C = _lst[i].d[j].C;
                    et1.N = _lst[i].d[j].N;
                    et1.JD = _lst[i].d[j].JD;
                    et1.ZD = _lst[i].d[j].ZD;
                    et1.Stime = _lst[i].Ptime;
                    list.Add(et1);
                }//按时间条件的帅选
                redcount = list.Count();
                szcount = list.Where(x => x.ZD > 0).ToList().Count();  //上涨数量
                xdcount = list.Where(x => x.ZD < 0).ToList().Count();  //下跌数量
                count10 = list.Where(x => x.ZD > 10).ToList().Count(); //最大涨幅超过100%
                CGL = ((Convert.ToDouble(szcount) / Convert.ToDouble(redcount)) * 100).ToString("f");  //成功率
            }


            _totalCount = list.Count();
            //lab_count = _totalCount + "";
            if (_totalCount > 0)
            {
                if ((_totalCount % _pagesize) == 0)
                    _totalPage = _totalCount / _pagesize;
                else
                    _totalPage = (int)(_totalCount / _pagesize) + 1;
            }

            if (page == 0)
                page = 1;
            if (page > _totalPage)
                page = _totalPage;
            int _index = (page - 1) * _pagesize;
            int _end = page * _pagesize;
            if (page == _totalPage && (_totalCount % _pagesize) != 0)
                _end = (page - 1) * _pagesize + _totalCount % _pagesize;

          
            for (int j = _index; j < _end; j++)
            {

                sb.AppendLine("<tr  style=\"height:15px;line-height:15px;\">");
                sb.AppendLine("<td class=\"wh1\">" + (j + 1) + "</td><td class=\"wh2\">" + list[j].N + "</td>");
                sb.AppendLine("<td class=\"wh3\">" + list[j].C + "</td><td class=\"wh4\">" + list[j].ZD + "%</td><td class=\"wh5\">" + list[j].Stime.ToString("yyyy-MM-dd") + "</td>");
                sb.AppendLine("</tr>");
                sb.AppendLine("<tr><td colspan=\"5\">");
                sb.AppendLine("<div class=\"clr\"></div>");
                sb.AppendLine("<div class=\"xian\"></div>");
                sb.AppendLine("</td></tr>");

            }

            sb.AppendLine("</table>");
            if (_totalPage > 1)
                strPage = "<div class=\"news_next\" style=\"white-space:nowrap\">" + PageHelper.fy(_totalCount, _pagesize, page, Request.Url.AbsoluteUri) + "</div>";

            return sb.ToString();
        }
        catch
        {
            sb.AppendLine("<tr colspan=\"5\"><td  style=\"padding-left:230px;padding-top:60px\">--暂无相关数据--</td></tr>");
            sb.AppendLine("</table>");
            return sb.ToString();
        }
    }


    /// <summary>
    ///Stock 的摘要说明
    ///股票池
    /// </summary>
    public class StockEntity
    {
        /// <summary>
        /// 日期
        /// </summary>
        public string T
        {
            get;
            set;
        }

        public DateTime Ptime
        {
            get
            {
                int count = T.Length;
                string y = T.Substring(0, 4);
                string m = T.Substring(4, 2);
                string d = T.Substring(6, 2);
                string tmpstr = y + "-" + m + "-" + d;
                string tmptime = Convert.ToDateTime(tmpstr).ToString("yyyy-MM-dd");
                return Convert.ToDateTime(tmptime);
            }
        }
        public List<StockList> d
        {
            get;
            set;
        }

    }

    public class StockList
    {
        /// <summary>
        /// 股票编号
        /// </summary>
        public string C
        {
            get;
            set;
        }
        /// <summary>
        /// 股票名称
        /// </summary>
        public string N
        {
            get;
            set;
        }

        /// <summary>
        /// 阶段涨幅
        /// </summary>
        public double JD
        {

            get;
            set;
        }
        /// <summary>
        /// 最大涨幅
        /// </summary>
        public double ZD
        {
            get;
            set;

        }
    }

    public class StockList2
    {
        /// <summary>
        /// 股票编号
        /// </summary>
        public string C
        {
            get;
            set;
        }
        /// <summary>
        /// 股票名称
        /// </summary>
        public string N
        {
            get;
            set;
        }

        /// <summary>
        /// 阶段涨幅
        /// </summary>
        public double JD
        {

            get;
            set;
        }
        /// <summary>
        /// 最大涨幅
        /// </summary>
        public double ZD
        {
            get;
            set;

        }
        public DateTime Stime
        {
            get;
            set;
        }
    }


    public int ToInt(object obj, int v)
    {
        try
        {
            v = int.Parse(obj.ToString());
        }
        catch { }
        return v;
    }
   
}